package io.dataease.auth.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import io.dataease.dto.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class JwtUtil {
    // 过期时间30分钟
    public static final long EXPIRE_TIME = 30 * 60 * 1000;

    public static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALY0BLGSC1vJ7KDYfu2KwNJExdzKazEuljXrFg/otYMap9hf6zioegFWEJYyZmfVmzB3yKcZQjWkNqx6iUT0ZlsqkZ3QWgRghtmNMEwmAh/ouMrwQUJEYDUOWi/HO6gVm68ftiF84ojm3d742Z2z6OPjrMowPJwVe0rawnjtzVZNAgMBAAECgYB6O3+CmNVh6J8EcPkF5vXgmh6bNnie7//5gBK27YkHwRQlBoRI6E2hI9nQyVMGCbko1NMZtspCLj43i6UXS1Z2ZILIa0DP5BT5iWPKvFo4zZzzA264/fK/ibfAVYyZypqv4fLomo+DnactAc2yyelS9+F4pRU5z0lVJKsqwar7CQJBAOLJv1mBKsaIaHi9Tsqn1YOEWHKWZ6gyHI8kUUferPzMq/xeu265x6YwaE65gWcQbZ1VkvQ4m7CHMtUfp3WH3jsCQQDNrBnd9adqnZPrsb7s2x3UWYLB2j+DY3skXnzj4WpuYztJzSo8deGPzZ/KTyGN9EEeSit+IIoEKYgqtomFGC0XAkAVaOxqm7oZEgmi1GRa12tdxiG/d5mDlG+Dx+7wdQIpv3zBD4RsWJeTheBjBjpEK5Xdk5WS/sEb8jeHTTy26MO/AkEAg3PO6JVcloD2jPsC+2GktBctwnaah4BqsTkO+5sUKk+UEjxmFVSGgSG8UlK3MIJ/qDthhvCHJdfhpuMaDA4CqwJAOb2rI7HBjMf4WMsTtTy8LJQbrOzapvhkh0dzXE8hsZmKVkprHwVbfPvNKlYePVdiPOEIhoZotixrObMnnHWPDw==";

    public static final String SECRET = "token";

    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    private static String getKeyValue(String token, String key) {
        try {
            //如果以那个Bearer
            if (token != null && !StringUtils.isEmpty(token) && token.startsWith("Bearer")) {
                token = token.substring(6);
            }
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim(key).asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static SysUser getLoginUser() {
        //2.正常的业务系统
        String token;
        SysUser sysUser = new SysUser();
        sysUser.setLoginName("admin");
        sysUser.setId("admin");
        try {
            token = JwtUtil.getKVByHeadr("Authorization");
        } catch (Exception e) {
            return sysUser;
        }
        try {
            builderSysUserBytoken(sysUser, token);
        } catch (Exception e) {
            genDefaultAdmin(sysUser);
        }
        return sysUser;
    }

    public static void genDefaultAdmin(SysUser sysUser) {
        sysUser.setLoginName("admin");
        sysUser.setId("ae17a484bc7c2e5222adbccd651b6feb");
        sysUser.setJwtId("ae17a484bc7c2e5222adbccd651b6feb");
        sysUser.setUserName("admin");
        sysUser.setSysDepartCode("001");
    }

    public static void builderSysUserBytoken(SysUser sysUser, String token) {
        sysUser.setLoginName(JwtUtil.getKeyValue(token, "loginName"));
        sysUser.setId(JwtUtil.getKeyValue(token, "userId"));
        sysUser.setJwtId(JwtUtil.getKeyValue(token, "jwtId"));
        sysUser.setUserName(JwtUtil.getKeyValue(token, "userName"));
        sysUser.setSysDepartCode(JwtUtil.getKeyValue(token, "sysDepartCode"));
    }


    /**
     * 根据request中的key获取value
     *
     * @param name
     * @return
     * @throws
     */
    private static String getKVByHeadr(String name) {
        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request == null) {
            return "";
        }
        String value = request.getHeader(name);
        return value;
    }

}
